package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Friend;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.FriendService;
import com.itheima.vo.ContactVo;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserCommentVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class MessageManager {

    @DubboReference
    private UserInfoService userInfoService;

    @DubboReference
    private QuestionService questionService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    // 好友申请
    public void strangerQuestions(long jiarenId, String reply) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.获取登录人userInfo
        UserInfo userInfo = userInfoService.findByUserId(userId);
        // 3.查询佳人的陌生人问题
        Question question = questionService.findByUserId(jiarenId);
        if (question == null) {
            question = new Question();
        }
        // 4.组装json
        Map map = new HashMap();
        map.put("userId", userId);
        map.put("huanXinId", "HX" + userId);
        map.put("nickname", userInfo.getNickname());
        map.put("strangerQuestion", question.getStrangerQuestion());
        map.put("reply", reply);
        String json = JSON.toJSONString(map);
        // 5.通过环信向对方发送消息
        huanXinTemplate.sendMsg("HX" + jiarenId, json);
    }

    @DubboReference
    private FriendService friendService;

    // 添加好友
    public void addContacts(Long friendId) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.mongo添加好友
        friendService.addContacts(userId, friendId);
        // 3.环信添加好友
        huanXinTemplate.addContacts("HX" + userId, "HX" + friendId);

    }

    // 查看好友列表
    public ResponseEntity findFriendByPage(Integer pageNum, Integer pageSize) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.调用rpc查询好友分页对象
        PageBeanVo pageBeanVo = friendService.findFriendByPage(userId, pageNum, pageSize);
        // 3.封装vo
        // 3-1 声明voList
        List<ContactVo> voList = new ArrayList<>();
        // 3-2 获取好友list
        List<Friend> friendList = (List<Friend>) pageBeanVo.getItems();
        // 3-3 遍历好友list
        if (CollectionUtil.isNotEmpty(friendList)) {
            for (Friend friend : friendList) {
                // 查询好友userInfo
                UserInfo userInfo = userInfoService.findByUserId(friend.getFriendId());
                // 创建vo
                ContactVo vo = new ContactVo();
                vo.setUserInfo(userInfo);  // 好像基本信息
                vo.setUserId("HX" + friend.getFriendId()); // 好友的环信id
                // 添加到集合
                voList.add(vo);
            }
        }
        // 4.将voList设置到分页对象
        pageBeanVo.setItems(voList);

        // 5.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }

    @DubboReference
    private CommentService commentService;

    // 根据类型查询我的点赞、评论、喜欢信息
    public ResponseEntity findMovmenetComment(Integer commentType, Integer pageNum, Integer pageSize) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.调用rpc查询
        PageBeanVo pageBeanVo = commentService.findMyCommentByPage(userId, commentType, pageNum, pageSize);
        // 3.封装 vo
        // 3-1 声明voList
        List<UserCommentVo> voList = new ArrayList<>();
        // 3-2 获取评论list
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        // 3-3 遍历评论list
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                // 查询操作人的userInfo
                UserInfo userInfo = userInfoService.findByUserId(comment.getUserId());
                //  创建vo
                UserCommentVo vo = new UserCommentVo();
                vo.setId(userInfo.getId().toString()); // 操作人id
                vo.setNickname(userInfo.getNickname()); // 操作人昵称
                vo.setAvatar(userInfo.getAvatar()); // 操作人头像
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated()))); // 操作时间
                // 添加到集合
                voList.add(vo);
            }
        }
        // 4.将volist设置到分页对象
        pageBeanVo.setItems(voList);

        // 5.返回分页对象
        return ResponseEntity.ok(pageBeanVo);
    }
}
